package com.crm.service.impl.customerManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.dao.mapper.contactManager.ContactMapper;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerPoolCustomMapper;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.ModificationRecordCustomMapper;
import com.crm.dao.mapper.customerManager.*;
import com.crm.model.dto.CustomerPoolDto;
import com.crm.model.entity.contactManager.Contact;
import com.crm.model.entity.customerManager.CustomerPool;
import com.crm.model.entity.customerManager.CustomerPoolExample;
import com.crm.dao.mapper.customerManager.CustomerPoolMapper;
import com.crm.model.entity.customerManager.*;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.customerManager.CustomerPoolQO;
import com.crm.model.vo.cluePoolManager.UserCompanyVO;
import com.crm.model.vo.customerManager.CustomerPoolVO;
import com.crm.model.vo.customerManager.ModificationRecordVO;
import com.crm.service.customerManager.CustomerPoolDbService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/15 10:11
 */
@Service
public class CustomerPoolDbServiceImpl implements CustomerPoolDbService {
    @Autowired
    CustomerPoolCustomMapper customerPoolCustomMapper;

    @Autowired
    CustomerPoolMapper customerPoolMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CustomerCompanyInfoMapper customerCompanyInfoMapper;

    @Autowired
    private CustomerContactPersonMapper customerContactPersonMapper;

    @Autowired
    private ContactMapper contactMapper;

    @Autowired
    private ModificationRecordMapper modificationRecordMapper;

    @Autowired
    private ModificationRecordCustomMapper modificationRecordCustomMapper;

    @Override
    public List<CustomerPoolVO> selectAllCustomerPoolList(CustomerPoolQO customerPoolQO, Integer dateUnit, String sortOrder) {
        return customerPoolCustomMapper.selectAllCustomerPoolList(customerPoolQO,dateUnit,sortOrder);
    }

    @Override
    public List<CustomerPoolVO> selectNormalCustomerPoolList(CustomerPoolQO customerPoolQO, Integer dateUnit, String sortOrder, String companyId) {
        return customerPoolCustomMapper.selectNormalCustomerPoolList(customerPoolQO,dateUnit,sortOrder,companyId);
    }

    @Override
    public CustomerPool selectCustomerPoolByName(String customerPoolName) {
        CustomerPoolExample customerPoolExample = new CustomerPoolExample();
        CustomerPoolExample.Criteria criteria = customerPoolExample.createCriteria();
        criteria.andCustomerPoolNameEqualTo(customerPoolName);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerPool> customerPoolList =  customerPoolMapper.selectByExample(customerPoolExample);
        if(customerPoolList.size()>0){
            return customerPoolList.get(0);
        }else {
            return null;
        }
    }
    public List<Customer> selectCustomerPoolVOList(CustomerPoolQO customerPoolQO, String sortOrder) {
        return null;
    }

    /**
     * 根据客户id查询客户详细信息
     * @param customer_id 客户id
     * @return
     */
    @Override
    public Customer customerHighSeasDetail(String customer_id) {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        criteria.andCustomerIdEqualTo(customer_id);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Customer> list = customerMapper.selectByExample(customerExample);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    @Override
    public Boolean insertCustomerPool(CustomerPool customerPool) {
        Boolean result = false;
        int num = customerPoolMapper.insertSelective(customerPool);
        if(num>0){
            result = true;
        }
        return result;
    }

    @Override
    public Boolean updatePoolStateById(CustomerPool customerPool) {
        return customerPoolMapper.updateByPrimaryKeySelective(customerPool) > 0;
    }


    /**
     * 根据客户id查询公司信息
     * @param customer_id 客户id
     * @return
     */
    @Override
    public CustomerCompanyInfo getCompanyInfo(String customer_id) {
        CustomerCompanyInfoExample customerCompanyInfoExample = new CustomerCompanyInfoExample();
        CustomerCompanyInfoExample.Criteria criteria = customerCompanyInfoExample.createCriteria();
        criteria.andCustomerIdEqualTo(customer_id);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerCompanyInfo> list = customerCompanyInfoMapper.selectByExampleWithBLOBs(customerCompanyInfoExample);
        if(list.size()>0){
            return list.get(0);
        }else {
            return new CustomerCompanyInfo();
        }
    }

    /**
     * 根据客户id查询公司联系人信息
     * @param customer_id 客户id
     * @param isSeniorExecutive 高管状态 0高管 1非高管
     * @param abolishState 废止状态  1不查询废止的 2查询废止的
     * @return
     */
    @Override
    public List<CustomerContactPerson> customerContactPersonList(String order,String customer_id, int isSeniorExecutive,int abolishState) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        customerContactPersonExample.setOrderByClause(order);
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customer_id);
        Boolean ifAdmin = CommonUtils.isAdmin(userAccount.getUserId());
//        if(!ifAdmin && !"9fb1c999393311eb9e0a00163e08893d".equals(userAccount.getCompanyId())) {//测试服务器内训部ID
        if(!ifAdmin && !"a5d32daa4c4441a395b521685d7ee4fa".equals(userAccount.getCompanyId())) {
            criteria.andCompanyIdEqualTo(userAccount.getCompanyId());
        }
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        if(isSeniorExecutive == 0){
            criteria.andIsSeniorExecutiveEqualTo(isSeniorExecutive);
        }
        if(abolishState == 1) {
            criteria.andAbolishStateEqualTo(abolishState);
        }
        List<CustomerContactPerson> list = customerContactPersonMapper.selectByExample(customerContactPersonExample);
        return list;
    }

    @Override
    public List<CustomerContactPerson> selectContactPersonListByCustomerId(String customerId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        //废止状态 已启动
        criteria.andAbolishStateEqualTo(1);
        criteria.andCompanyIdEqualTo(userAccount.getCompanyId());
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        customerContactPersonExample.setOrderByClause("create_tm desc");
        List<CustomerContactPerson> list = customerContactPersonMapper.selectByExample(customerContactPersonExample);
        return list;
    }

    /**
     * 客户详情-新增客户下的联系人
     * @param customerContactPerson 客户联系人实体
     * @return
     */
    @Override
    public Boolean addCustomerContactPerson(CustomerContactPerson customerContactPerson) {
        Boolean result = false;
        int num = customerContactPersonMapper.insertSelective(customerContactPerson);
        if(num>0){
            result = true;
        }
        return result;
    }



    /**
     * 客户详情-新增联系人管理的联系人
     * @param contact 联系人实体
     * @return
     */
    @Override
    public Boolean addContact(Contact contact) {
        Boolean result = false;
        int num = contactMapper.insertSelective(contact);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 根据联系人id,回显联系人信息
     * @param customerContactPersonId 客户联系人id
     * @return
     */
    @Override
    public CustomerContactPerson getCustomerContactPerson(String customerContactPersonId) {
        return customerContactPersonMapper.selectByPrimaryKey(customerContactPersonId);
    }

    /**
     *客户详情-修改联系人
     * @param customerContactPerson 客户联系人实体
     * @return
     */
    @Override
    public boolean updateCustomerContactPerson(CustomerContactPerson customerContactPerson) {
        Boolean result = false;
        int num = customerContactPersonMapper.updateByPrimaryKeySelective(customerContactPerson);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 把修改的数据存放到修改信息表里
     * @param modificationRecord 修改记录实体
     * @return
     */
    @Override
    public boolean addModificationRecord(ModificationRecord modificationRecord) {
        Boolean result = false;
        int num = modificationRecordMapper.insertSelective(modificationRecord);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 根据客户id查询公司修改记录
     * @param order 分组类型
     * @param customerId 客户id
     * @return
     */
    @Override
    public List<ModificationRecordVO> modificationRecordList(String order, String customerId) {
        List<ModificationRecordVO> list = modificationRecordCustomMapper.modificationRecordList(order,customerId,Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return list;
    }

    @Override
    public Boolean isCustomerPoolRepeated(String customerPoolName, String customerPoolId) {
        Boolean result = false;
        CustomerPoolExample customerPoolExample = new CustomerPoolExample();
        CustomerPoolExample.Criteria criteria = customerPoolExample.createCriteria();
        criteria.andCustomerPoolNameEqualTo(customerPoolName);
        criteria.andCustomerPoolIdNotEqualTo(customerPoolId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerPool> customerPoolList =  customerPoolMapper.selectByExample(customerPoolExample);
        if(customerPoolList.size()>0){
            result = true;
        }
        return result;
    }

    /**
     * 客户详情--联系人 开启/废止联系人
     * @param customerContactPerson 客户联系人实体
     * @return
     */
    @Override
    public Boolean updateAbolishStateById(CustomerContactPerson customerContactPerson) {
        return customerContactPersonMapper.updateByPrimaryKeySelective(customerContactPerson) > 0;
    }

    /**
     * 客户详情
     * @param customerId 客户id
     * @return
     */
    @Override
    public Customer getCustomer(String customerId) {
        return customerMapper.selectByPrimaryKey(customerId);
    }

    /**
     * 修改客户信息
     * @param customer 客户实体
     * @return
     */
    @Override
    public boolean updateCustomer(Customer customer) {
        Boolean result = false;
        int num = customerMapper.updateByPrimaryKeySelective(customer);
        if(num>0){
            result = true;
        }
        return result;
    }

    @Override
    public CustomerPool selectCustomerPoolById(String customerPoolId) {
        return customerPoolMapper.selectByPrimaryKey(customerPoolId);
    }

    /**
     * 根据客户池ID查询公司
     * @param customerPoolId  ---客户池ID
     * @return
     */
    @Override
    public List<UserCompanyVO> selectDistributeCompany(String customerPoolId) {
        return customerPoolCustomMapper.selectDistributeCompany(customerPoolId,(short)StatusCodeEnum.NORMAL_CODE.getState());
    }

    /**
     * 根据用户名查询用户信息及所在公司信息(全国公有池、分公司公有池-分配-按人员姓名模糊查找)
     * @param userName ---用户名
     * @return
     */
    @Override
    public List<UserCompanyVO> selectUserAndCompany(String userName) {
        return customerPoolCustomMapper.selectUserAndCompany(userName);
    }

    /**
     * 查询分公司公有池的用户
     * @param companyIds ---公司ID集合
     * @param userName ---用户名
     * @return
     */
    @Override
    public  List<UserCompanyVO> selectUserByPoolPermission(List<String> companyIds,String userName) {
        return customerPoolCustomMapper.selectUserByPoolPermission(companyIds,userName);
    }

    @Override
    public List<CustomerPool> selectAllEnableCustomerPoolName() {
        return customerPoolCustomMapper.selectAllEnableCustomerPoolName();
    }

    @Override
    public List<CustomerPool> selectPartEnableCustomerPoolName(String companyId) {
        return customerPoolCustomMapper.selectPartEnableCustomerPoolName(companyId);
    }

    /**
     * 根据客户池id更新客户池
     * @Author pym
     * @Date  2021/6/17 15:49
     * @param customerPool 客户池
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean updateCustomerPoolById(CustomerPool customerPool) {
        return customerPoolMapper.updateByPrimaryKeyWithBLOBs(customerPool) > 0;
    }

    /**
     * 根据公司id查询客户池
     * @Author pym
     * @Date  2021/7/5 16:20
     * @param companyId 公司Id
     * @return {@link {com.crm.model.entity.customerManager.CustomerPool}}
     */
    @Override
    public CustomerPool selectCustomerPoolCompanyId(String companyId) {
        CustomerPoolExample customerPoolExample = new CustomerPoolExample();
        CustomerPoolExample.Criteria criteria = customerPoolExample.createCriteria();
        criteria.andAffiliatedCompanyIdEqualTo(companyId);
        criteria.andNatureEqualTo(1);//全国公有池
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerPool> customerPoolList =  customerPoolMapper.selectByExample(customerPoolExample);
        if(customerPoolList.size()>0){
            return customerPoolList.get(0);
        }
        return null;
    }

    @Override
    public CustomerPoolDto selectPoolByName(String name) {
        return customerPoolMapper.selectPoolByName(name);
    }
}
